package com.huonan.jb.tools.yaml;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description yaml处理
 * @Author huonan
 * @Date 2021/5/1 3:49 下午
 * @Version V1.0
 **/
public class YamlHander {

    public static void main(String[] args) {
        String path = "/Users/lxp/IdeaProjects/Work/schema-mapping/artifacts/qingbo/video-douyin.com-data.yaml";
        try {
            Map<String, Object> mappings = getMappings(path);
            Map source = (Map) mappings.get("mappings");
            System.out.println(source.keySet().size());
            Map filter = filter(source);
            System.out.println(filter.keySet().size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Map<String, Object> filter(Map<String, Object> source) {
        Map<String, Object> newMap = new HashMap<>();
        for (Map.Entry<String, Object> entry : source.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof LinkedHashMap) {
                if (!((LinkedHashMap<?, ?>) value).containsKey("tmp")) {
                    newMap.put(entry.getKey(), value);
                }else {
                    System.out.println(value);
                }
            } else {
                newMap.put(entry.getKey(), value);
            }
        }
        return newMap;
    }

    public static Map<String, Object> getMappings(String yamlPath) throws IOException {
        Map<String, Object> m1;
        /* 读取 */
        Yaml y = new Yaml();
        //创建file对象
//        File file = new File("JavaUtils/data/yaml/wenda-common-data.yaml");
        File file = new File(yamlPath);
        m1 = y.load(Files.newInputStream(file.toPath()));
        return m1;
    }
}
